package com.gtcom.sdk.utils;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Random;

/**
 * 签名工具类
 */
public class SignUtils {

    /**
     * md5 加密
     * @param string
     * @return
     */
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取10位随机数
     * @return 随机数
     */
    public static String getRandom() {
        String randomStr = "";
        Random random = new Random(new Date().getTime());
        for(int i = 0 ; i < 10; i ++){
            randomStr += random.nextInt(10);
        }
        return randomStr;
    }

    /**
     * 获取签名
     * @param appkey appkey
     * @param secret 秘钥
     * @param salt 盐值（10位随机数）
     * @return 签名
     */
    public static String getSign(String appkey, String secret, String salt) {
        return md5(appkey + secret + salt);
    }

}
